package team.hyznrj.studentsys.utils;

import java.util.concurrent.atomic.AtomicInteger;

public class SerialGenerator {
	private final AtomicRangeInteger atomicRangeInteger = new AtomicRangeInteger(0, 1 << 32 - 1);

	public long getSerialId() {
		final long currentTimeMillis = System.currentTimeMillis();
		return (long) (currentTimeMillis << 32 | atomicRangeInteger.incrAndGet());
	}
}
class AtomicRangeInteger {
	private final int maxValue;
	private final AtomicInteger atomicInteger;

	public AtomicRangeInteger(final int minValue, final int maxValue) {
		this.atomicInteger = new AtomicInteger(minValue);
		this.maxValue = maxValue;
	}

	public Integer incrAndGet() {
		final int value = this.atomicInteger.incrementAndGet();
		if (value > maxValue) {
			return null;
		}
		return value;
	}
}
